<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\apple\controller;
use app\api\model\v1\Common as apiCommonModel;
use think\facade\Db;
use think\facade\View;

class About extends Base{
	//列表页
    public function index(){
		//三级分类嵌套循环
		$sortWhere[] = ['lang','=',$this->lang]; $sortWhere[] = ['parentid','=',0]; $sortWhere[] = ['tabledir','=',$this->controller];
		$sortlist = sublist('sort',$sortWhere,1000,'sequence desc',$this->controller);
		View::assign('sortlist',$sortlist);

        //内页banner
        $pageWhere[] = ['signid','=',1]; $pageWhere[] = ['lang','=',$this->lang];
        $pageWhere[] = ['position','=',$this->controller]; $pageWhere[] = ['att_type','=','banner'];
		$adRs = Db::name('ad')->where($pageWhere)->field('thumb')->find();
		$pageBanner = !empty($adRs) ? $adRs['thumb'] : '';

		//模块信息
		$moduleWhere[] = ['tabledir','=',$this->controller]; $moduleWhere[] = ['lang','=',$this->lang];
		$moduleRs = Db::name('module')->where($moduleWhere)->find();
		//模块未启用则重定向到首页
		if($moduleRs['disabled'] == 0) $this->redirect(request()->domain(),301);
		//模块是否启用相关属性：是否启用价格等
        $isdisplay_array = explode(',',$moduleRs['isdisplay']);
        View::assign('isdisplay_array',$isdisplay_array);
        
		// 城市分站TDK
		if(!empty($this->cityRs)){
		    $title = str_replace('[city]',$this->cityRs['abbreviation'],$moduleRs['seotitle']);
		    $keyword = str_replace('[city]',$this->cityRs['abbreviation'],$moduleRs['keyword']);
		    $description = str_replace('[city]',$this->cityRs['abbreviation'],$moduleRs['description']);
		}else{
		    $title = str_replace('[city]','',$moduleRs['seotitle']);
		    if(strpos($title,'-') !== false){
		        $titleArray = explode('-',$title);
		        $titleArray[sizeof($titleArray)-1] = $this->siteRs['company'];
		        $title = implode('-',$titleArray);
		    }
		    $keyword = str_replace('[city]','',$moduleRs['keyword']);
		    $description = str_replace('[city]','',$moduleRs['description']);
		}

        // 分类
		$sortid = input('sortid'); $template = 'list_default';//列表页模板
		if(!empty($sortid)){
            $sortRs =  Db::name('sort')->where('id',$sortid)->find();
            $title = empty($sortRs['seotitle']) ? $sortRs['title'] : $sortRs['seotitle'];
            $keyword = $sortRs['keyword']; $description = $sortRs['description'];
            // 城市分站TDK
            if(!empty($this->cityRs)){
                $titleArray = explode('-',$title);
                if(count($titleArray) == 1){
                    $title = $this->cityRs['abbreviation'].$title;
                }else{
                    $title = '';
                    foreach ($titleArray as $str){
                        $title .= $this->cityRs['abbreviation'].$str.'-';
                    }
                    $title = rtrim($title,'-');
                }
                
                $keywordArray = explode('，',$keyword);
                if(count($keywordArray) == 1){
                    $keyword = $this->cityRs['abbreviation'].$keyword;
                    $description = str_replace($keyword,$this->cityRs['abbreviation'].$keyword,$description);
                }else{
                    $keyword = '';
                    foreach ($keywordArray as $str){
                        $keyword .= $this->cityRs['abbreviation'].$str.'，';
                        $description = str_replace($str,$this->cityRs['abbreviation'].$str,$description);
                    }
                    $keyword = rtrim($keyword,'，');
                }
            }
			//内页banner
		    if(!empty($sortRs['thumb'])) $pageBanner = $sortRs['thumb'];
		    //列表页模板
		    $template = $sortRs['listtemp'];
		}else{
			$sortRs = array('title'=>lang('v_about'));
		}

		View::assign('page_banner',$pageBanner);//内页banner
		View::assign('title',$title);
		View::assign('keyword',$keyword);
		View::assign('description',$description);
		View::assign('sortRs',$sortRs);
		
		//面包屑导航
		$crumb = apiCommonModel::crumb('sort',$this->controller,$sortid,$this->lang);
		View::assign('crumb',$crumb);
		
		//后台模块设置的前端显示数量
        $listNum = !empty($moduleRs['listnum']) ? $moduleRs['listnum'] : 12;

		//列表
		$order = ["$this->controller.sequence"=>'desc',"$this->controller.update_time"=>'desc'];
		$rs = apiCommonModel::index($this->controller,$sortid,'*',$this->lang,$listNum,$order,1,trim(input('keyword')));
		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('last_page',$rs->lastPage());

        return view($template);
    }

    //详情页
    public function view(){
		$rs = apiCommonModel::view(input('id'),$this->controller,$this->userid,$this->lang);
        //信息不存在或已删除
        if(empty($rs)) $this->redirect(request()->domain(),301);

        View::assign('rs',$rs);
        
        // 所属分类
        $sortRs =  Db::name('sort')->where('id',$rs['sortid'])->find();
        View::assign('sortRs',$sortRs);
        
		//SEO标题
		$title = empty($rs['seotitle']) ? $rs['title'] : $rs['seotitle'];
		// 城市分站TDK
        if(!empty($this->cityRs)){
            if(empty($rs['city_title'])){
                $titleArray = explode('-',$title);
                if(count($titleArray) == 1){
                    $title = $this->cityRs['abbreviation'].$title;
                }else{
                    $title = '';
                    foreach ($titleArray as $str){
                        $title .= $this->cityRs['abbreviation'].$str.'-';
                    }
                    $title = rtrim($title,'-');
                }
            }else{
                $title = str_replace('[city]',$this->cityRs['abbreviation'],$rs['city_title']);
            }
        }
        View::assign('title',$title);
            
		//更新浏览次数
		Db::name($this->controller)->where('id',$rs['id'])->inc('hits')->update();
		
		//面包屑导航
		$crumb = apiCommonModel::crumb('sort',$this->controller,$rs['sortid'],$this->lang);
		View::assign('crumb',$crumb);

		//三级分类嵌套循环
		$subWhere[] = ['lang','=',$this->lang]; $subWhere[] = ['parentid','=',0]; $subWhere[] = ['tabledir','=',$this->controller];
		$sortlist = sublist('sort',$subWhere,1000,'sequence desc');
		View::assign('sortlist',$sortlist);


		//属性
		View::assign('attribute',json_decode($rs['attvalue'],true));

		//内页banner
        $pageWhere[] = ['signid','=',1]; $pageWhere[] = ['lang','=',$this->lang];
        $pageWhere[] = ['position','=',$this->controller]; $pageWhere[] = ['att_type','=','banner'];
		$adRs = Db::name('ad')->where($pageWhere)->field('thumb')->find();
		$pageBanner = !empty($adRs) ? $adRs['thumb'] : '';
		$aboutRs = Db::name($this->controller)->where('id',$rs['id'])->field('thumb')->find();
		if(!empty($sortRs['thumb'])) $pageBanner = $sortRs['thumb'];
		View::assign('page_banner',$pageBanner);

        //子类找父类，相关单页的查询条件
        $sortidArr = ""; $sortWhere[] = ['tabledir','=',$this->controller]; $sortWhere[] = ['lang','=',$this->lang];
		$sortData = getParentSort(Db::name('sort')->where($sortWhere)->select(),$rs['sortid']);
        foreach($sortData as $k => $w){
			$k<count($sortData)-1 ? $sortidArr.=$w['id'].',' : $sortidArr.=$w['id'];
        }
		if(empty($sortidArr)) $sortidArr = '1,1';
		View::assign('sortidArr',$sortidArr);

		//模块是否启用相关属性：是否启用价格等
		$isdisplay = Db::name('module')->where($sortWhere)->value('isdisplay');
        View::assign('isdisplay_array',explode(',',$isdisplay));

	    $field = 'id,title,thumb,keyword,description';
		$where[] = ['sortid','in',$sortidArr]; $where[] = ['signid','=',1]; $where[] = ['lang','=',$this->lang];
	    // 上一篇
	    $preRs = Db::name($this->controller)->where('addtime','<',$rs['addtime'])->where($where)->field($field)->find();
	    View::assign('preRs',$preRs);
	    
	    // 下一篇
        $nextRs = Db::name($this->controller)->where('addtime','>',$rs['addtime'])->where($where)->field($field)->find();
	    View::assign('nextRs',$nextRs);
		
		//更多图片
		View::assign('morepics',explode('|',$rs['morepics']));
		
        //模板
		$template = !empty($rs['viewtemp']) ? $rs['viewtemp'] : 'view_default';

        return view($template);
	}
}